﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using NITGEN.SDK.NBioBSP;
using Abhiruchi_Phase1.BAL_DAL.ORG_DAL;
using Abhiruchi_Phase1.BAL_DAL.Biometric_DAL;


namespace Abhiruchi_Phase1
{
    /// <summary>
    /// Interaction logic for BIO_DeviceMaster.xaml
    /// </summary>
    public partial class BIO_DeviceMaster : Window
    {
        bool clearClicked = false;
        public BIO_DeviceMaster()
        {
            InitializeComponent();
            LoadFormData();
        }
        private void LoadFormData()
        {
            //loadingGif.Visibility = Visibility.Hidden;
            txtDeviceName.IsEnabled = false;
            cmbDeviceID.IsEnabled = false;
            cmbSection.IsEnabled = false;
            btnCheck.Focus();
            DataTable mydtSec = new DataTable();
            mydtSec = ORGTaskMappingMasterDAL.GetDBDetails(6);

            if (mydtSec.Rows.Count > 0)
            {
                cmbSection.DataContext = mydtSec.DefaultView;
                cmbSection.DisplayMemberPath = mydtSec.Columns["SectionName"].ToString();
                cmbSection.SelectedValuePath = mydtSec.Columns["SectionId"].ToString();
            }

            DataTable mydt = BioDeviceInfoDAL.GetDeviceInfoFromDB(5);
            if (mydt.Rows.Count > 0)
            {
                grdDeviceInfo.ItemsSource = mydt;
            }
            chkIsActive.IsChecked = true;
        }

        private void btnCheck_Click(object sender, RoutedEventArgs e)
        {
            cmbDeviceID.IsEnabled = true;
            NBioAPI objAPI = new NBioAPI();
            uint deviceCount;
            short[] DeviceID;
            objAPI.EnumerateDevice(out deviceCount, out DeviceID);

            //if (btnCheck.ClickMode == ClickMode.Press)
            //{
            //    loadingGif.Visibility = Visibility.Visible;
            //}
            //else if (btnCheck.ClickMode == ClickMode.Release)
            //{
            //    loadingGif.Visibility = Visibility.Hidden;
            //}
            if (deviceCount != 0)
            {
                cmbSection.IsEnabled = true;
                if (cmbDeviceID.Items.Count == 0)
                {
                    for (int i = 0; i < deviceCount; i++)
                    {
                        cmbDeviceID.Items.Add(DeviceID[i]);
                    }
                }
            }
            else
            {
                cmbDeviceID.Items.Add("No device connected");
                cmbDeviceID.SelectedIndex = 0;
            }
        }

        private void cmbDeviceID_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataTable mydt = new DataTable();
            if (cmbDeviceID.SelectedItem != null)
            {
                mydt = BioDeviceInfoDAL.GetDevice(2, cmbDeviceID.SelectedItem.ToString());
                if (mydt.Rows.Count > 0)
                {
                    //cmbDeviceID.Text = mydt.Rows[0][0].ToString();
                    txtDeviceName.Text = mydt.Rows[0][0].ToString();
                    cmbSection.SelectedValue = Int32.Parse(mydt.Rows[0][1].ToString());
                    txtRemarks.Text = mydt.Rows[0][2].ToString();
                    if (Int32.Parse(mydt.Rows[0][3].ToString()) == 1)
                    {
                        chkIsActive.IsChecked = true;
                    }
                    else if (Int32.Parse(mydt.Rows[0][4].ToString()) == 0)
                    {
                        chkIsActive.IsChecked = false;
                    }
                    btnSave.Content = "Update";
                    btnCheck.IsEnabled = false;
                }
            }
        }
        private void cmbSection_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (clearClicked == false)
            {
                txtDeviceName.IsEnabled = true;
                txtDeviceName.Text = ((DataRowView)cmbSection.Items[cmbSection.SelectedIndex]).Row[1].ToString() + "_" + cmbDeviceID.SelectedItem;
            }

        }
        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            btnSave.Content = "Save";
            btnCheck.IsEnabled = true;
            clearClicked = true;
            //cmbDeviceID.SelectedItem = null;
            cmbDeviceID.Items.Clear();
            cmbSection.SelectedIndex = -1;
            txtDeviceName.Text = "";
            txtRemarks.Text = "";
            txtDeviceName.IsEnabled = false;
            cmbDeviceID.IsEnabled = false;
            cmbSection.IsEnabled = false;
            clearClicked = false;
            btnCheck.Focus();

        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (cmbDeviceID.SelectedValue != null && cmbSection.SelectedValue != null && txtDeviceName.Text != "")
            {
                var result = MessageBox.Show("Do you want to map the device to the section", "Mapping Device", MessageBoxButton.YesNo, MessageBoxImage.Information);
                if (result.ToString() == "Yes")
                {
                    int isActive;
                    if (btnSave.Content == "Update")
                    {
                        if (chkIsActive.IsChecked == true)
                        {
                            isActive = 1;
                        }
                        else
                        {
                            isActive = 0;
                        }
                        BioDeviceInfoDAL.UpdateDeviceInfo(4, cmbDeviceID.Text, txtDeviceName.Text, Int32.Parse(((DataRowView)cmbSection.Items[cmbSection.SelectedIndex]).Row[0].ToString()), txtRemarks.Text, isActive);
                        DataTable mydt = BioDeviceInfoDAL.GetDeviceInfoFromDB(5);
                        if (mydt.Rows.Count > 0)
                        {
                            grdDeviceInfo.ItemsSource = mydt;
                        }
                    }
                    else if (btnSave.Content == "Save")
                    {
                        if (chkIsActive.IsChecked == true)
                        {
                            isActive = 1;
                        }
                        else
                        {
                            isActive = 0;
                        }
                        DataTable mydt = new DataTable();
                        mydt = BioDeviceInfoDAL.SaveinDB(3, cmbDeviceID.Text, txtDeviceName.Text, Int32.Parse(((DataRowView)cmbSection.Items[cmbSection.SelectedIndex]).Row[0].ToString()), txtRemarks.Text, isActive);
                        if (mydt.Rows.Count > 0)
                        {
                            grdDeviceInfo.ItemsSource = mydt;
                        }
                    }
                }

            }
            else
            {
                MessageBox.Show("Device ID, Section & Device Names are mandatory");
            }
        }

        private void grdDeviceInfo_RowActivated(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
        {
            btnCheck.IsEnabled = false;
            btnSave.Content = "Update";
            cmbDeviceID.Items.Clear();
            if (grdDeviceInfo.SelectionMode == SelectionMode.Single)
            {
                cmbDeviceID.IsEnabled = true;
                cmbSection.IsEnabled = true;
                txtDeviceName.IsEnabled = true;
                cmbDeviceID.Items.Add(((DataRow)e.Row.DataContext).ItemArray[0].ToString());
                cmbDeviceID.SelectedIndex = 0;
                cmbSection.Text = ((DataRow)e.Row.DataContext).ItemArray[2].ToString();
                txtDeviceName.Text = ((DataRow)e.Row.DataContext).ItemArray[1].ToString();
                txtRemarks.Text = ((DataRow)e.Row.DataContext).ItemArray[3].ToString();
                if (Int32.Parse(((DataRow)e.Row.DataContext).ItemArray[4].ToString()) == 1)
                {
                    chkIsActive.IsChecked = true;
                }
                else if (Int32.Parse(((DataRow)e.Row.DataContext).ItemArray[4].ToString()) == 0)
                {
                    chkIsActive.IsChecked = false;
                }

            }
        }
    }
}
